home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src / htslib.h < prev    next >
C/C++ Source or Header  |  2007-06-15  |  24KB  |  640 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Subroutines .h                                         */
  34. /* Author: Xavier Roche                                         */
  35. /* ------------------------------------------------------------ */
  36.  
  37. // Fichier librairie .h
  38.  
  39. #ifndef HTS_DEFH
  40. #define HTS_DEFH 
  41.  
  42. /* Forward definitions */
  43. #ifndef HTS_DEF_FWSTRUCT_htsrequest
  44. #define HTS_DEF_FWSTRUCT_htsrequest
  45. typedef struct htsrequest htsrequest;
  46. #endif
  47. #ifndef HTS_DEF_FWSTRUCT_htsblk
  48. #define HTS_DEF_FWSTRUCT_htsblk
  49. typedef struct htsblk htsblk;
  50. #endif
  51. #ifndef HTS_DEF_FWSTRUCT_t_dnscache
  52. #define HTS_DEF_FWSTRUCT_t_dnscache
  53. typedef struct t_dnscache t_dnscache;
  54. #endif
  55.  
  56. /* dΘfinitions globales */
  57. #include "htsglobal.h"
  58.  
  59. /* basic net definitions */
  60. #include "htsbase.h"
  61. #include "htsbasenet.h"
  62. #include "htsnet.h"
  63. #include "htsdefines.h"
  64.  
  65. /* cookies et auth */
  66. #include "htsbauth.h"
  67.  
  68. // Attention, dΘfinition existante Θgalement dans le shell
  69. // (α modifier avec celle-ci)
  70. #define POSTTOK "?>post"
  71.  
  72. #include "htsopt.h"
  73.  
  74. #define READ_ERROR (-1)
  75. #define READ_EOF (-2)
  76. #define READ_TIMEOUT (-3)
  77. #define READ_INTERNAL_ERROR (-4)
  78.  
  79. /* concat */
  80.  
  81. #if ( defined(_WIN32) && defined(_MSC_VER) && ( _MSC_VER >= 1300 ) && (_MSC_VER <= 1310 ) )
  82. /* NOTE: VC2003 inlining bug in optim mode not respecting function call sequence point */
  83. #define MSVC2003INLINEBUG __declspec(noinline)
  84. #else
  85. #define MSVC2003INLINEBUG
  86. #endif
  87. MSVC2003INLINEBUG static char* getHtsOptBuff_(httrackp *opt) {
  88.     opt->state.concat.index = ( opt->state.concat.index + 1 ) % 16;
  89.     return opt->state.concat.buff[opt->state.concat.index];
  90. }
  91. #undef MSVC2003INLINEBUG
  92. #define OPT_GET_BUFF(OPT) ( getHtsOptBuff_(OPT) )
  93.  
  94. // structure pour paramΦtres supplΘmentaires lors de la requΩte
  95. #ifndef HTS_DEF_FWSTRUCT_htsrequest_proxy
  96. #define HTS_DEF_FWSTRUCT_htsrequest_proxy
  97. typedef struct htsrequest_proxy htsrequest_proxy;
  98. #endif
  99. struct htsrequest_proxy {
  100.   int active;
  101.   char name[1024];
  102.   int port;
  103.   char bindhost[256];   // bind this host
  104. }; 
  105. #ifndef HTS_DEF_FWSTRUCT_htsrequest
  106. #define HTS_DEF_FWSTRUCT_htsrequest
  107. typedef struct htsrequest htsrequest;
  108. #endif
  109. struct htsrequest {
  110.   short int user_agent_send;  // user agent (ex: httrack/1.0 [sun])
  111.   short int http11;           // l'en tΩte peut (doit) Ωtre signΘ HTTP/1.1 et non HTTP/1.0
  112.   short int nokeepalive;      // pas de keep-alive
  113.   short int range_used;       // Range utilisΘ
  114.   short int nocompression;    // Pas de compression
  115.   short int flush_garbage;    // recycled
  116.   char user_agent[128];
  117.   char referer[256];
  118.   char from[256];
  119.   char lang_iso[64];
  120.   htsrequest_proxy proxy;              // proxy
  121. };
  122.  
  123.  
  124. // structure pour retour d'une connexion/prise d'en tΩte
  125. #ifndef HTS_DEF_FWSTRUCT_htsblk
  126. #define HTS_DEF_FWSTRUCT_htsblk
  127. typedef struct htsblk htsblk;
  128. #endif
  129. struct htsblk {
  130.   int statuscode;        // status-code, -1=erreur, 200=OK,201=..etc (cf RFC1945)
  131.   short int notmodified; // page ou fichier NON modifiΘ (transfΘrΘ)
  132.   short int is_write;    // sortie sur disque (out) ou en mΘmoire (adr)
  133.   short int is_chunk;    // mode chunk
  134.   short int compressed;  // compressΘ?
  135.   short int empty;       // vide?
  136.   short int keep_alive;  // Keep-Alive?
  137.   short int keep_alive_trailers;  // ..with trailers extension
  138.   int keep_alive_t;      // KA timeout
  139.   int keep_alive_max;    // KA number of requests
  140.   char* adr;             // adresse du bloc de mΘmoire, NULL=vide
  141.   char* headers;         // adresse des en tΩtes si prΘsents
  142.   FILE* out;             // Θcriture directe sur disque (si is_write=1)
  143.   LLint size;            // taille fichier
  144.   char msg[80];          // message Θventuel si Θchec ("\0"=non prΘcisΘ)
  145.   char contenttype[64];  // content-type ("text/html" par exemple)
  146.   char charset[64];      // charset ("iso-8859-1" par exemple)
  147.   char contentencoding[64];  // content-encoding ("gzip" par exemple)
  148.   char* location;        // on copie dedans Θventuellement la vΘritable 'location'
  149.   LLint totalsize;       // taille totale α tΘlΘcharger (-1=inconnue)
  150.   short int is_file;     // ce n'est pas une socket mais un descripteur de fichier si 1
  151.   T_SOC soc;             // ID socket
  152.   SOCaddr address;       // IP address
  153.   int     address_size;  // IP address structure length
  154.   FILE* fp;              // fichier pour file://
  155. #if HTS_USEOPENSSL
  156.   short int ssl;         // is this connection a SSL one? (https)
  157.   // BIO* ssl_soc;          // SSL structure
  158.   SSL * ssl_con;         // connection structure
  159. #endif
  160.   char lastmodified[64]; // Last-Modified
  161.   char etag[64];         // Etag
  162.   char cdispo[256];      // Content-Disposition coupΘ
  163.   LLint  crange;         // Content-Range
  164.   int debugid;           // debug connection
  165.   /* */
  166.   htsrequest req;        // paramΦtres pour la requΩte
  167.   /*char digest[32+2];   // digest md5 gΘnΘrΘ par le moteur ("" si non gΘnΘrΘ)*/
  168. };
  169.  
  170.  
  171. /* ANCIENNE STURCTURE pour cache 1.0 */
  172. #ifndef HTS_DEF_FWSTRUCT_OLD_t_proxy
  173. #define HTS_DEF_FWSTRUCT_OLD_t_proxy
  174. typedef struct OLD_t_proxy OLD_t_proxy;
  175. #endif
  176. struct OLD_t_proxy {
  177.   int active;
  178.   char name[1024];
  179.   int port;
  180. }; 
  181. #ifndef HTS_DEF_FWSTRUCT_OLD_htsblk
  182. #define HTS_DEF_FWSTRUCT_OLD_htsblk
  183. typedef struct OLD_htsblk OLD_htsblk;
  184. #endif
  185. struct OLD_htsblk {
  186.   int statuscode;  // ANCIENNE STURCTURE - status-code, -1=erreur, 200=OK,201=..etc (cf RFC1945)
  187.   int notmodified; // ANCIENNE STURCTURE - page ou fichier NON modifiΘ (transfΘrΘ)
  188.   int is_write;    // ANCIENNE STURCTURE - sortie sur disque (out) ou en mΘmoire (adr)
  189.   char* adr;       // ANCIENNE STURCTURE - adresse du bloc de mΘmoire, NULL=vide
  190.   FILE* out;       // ANCIENNE STURCTURE - Θcriture directe sur disque (si is_write=1)
  191.   int size;        // ANCIENNE STURCTURE - taille fichier
  192.   char msg[80];    // ANCIENNE STURCTURE - message Θventuel si Θchec ("\0"=non prΘcisΘ)
  193.   char contenttype[64];  // ANCIENNE STURCTURE - content-type ("text/html" par exemple)
  194.   char* location;  // ANCIENNE STURCTURE - on copie dedans Θventuellement la vΘritable 'location'
  195.   int totalsize;   // ANCIENNE STURCTURE - taille totale α tΘlΘcharger (-1=inconnue)
  196.   int is_file;     // ANCIENNE STURCTURE - ce n'est pas une socket mais un descripteur de fichier si 1
  197.   T_SOC soc;       // ANCIENNE STURCTURE - ID socket
  198.   FILE* fp;        // ANCIENNE STURCTURE - fichier pour file://
  199.   OLD_t_proxy proxy;   // ANCIENNE STURCTURE - proxy
  200.   int user_agent_send;  // ANCIENNE STURCTURE - user agent (ex: httrack/1.0 [sun])
  201.   char user_agent[64];
  202.   int http11;           // ANCIENNE STURCTURE - l'en tΩte doit Ωtre signΘ HTTP/1.1 et non HTTP/1.0
  203. };
  204. /* fin ANCIENNE STURCTURE pour cache 1.0 */
  205.  
  206. // cache pour le dns, pour Θviter de faire des gethostbyname sans arrΩt
  207. #ifndef HTS_DEF_FWSTRUCT_t_dnscache
  208. #define HTS_DEF_FWSTRUCT_t_dnscache
  209. typedef struct t_dnscache t_dnscache;
  210. #endif
  211. struct t_dnscache {
  212.   char iadr[1024];
  213.   struct t_dnscache* n;
  214.   char host_addr[HTS_MAXADDRLEN];    // 4 octets (v4), ou 16 octets (v6)
  215.   int host_length;                   // 4 normalement - ==0  alors en cours de rΘsolution
  216.                                      // ou >16 si sockaddr
  217.                                      //                 ==-1 alors erreur (host n'Θxiste pas)
  218. };
  219.  
  220.  
  221. /* Library internal definictions */
  222. #ifdef HTS_INTERNAL_BYTECODE
  223.  
  224. extern htsmutex dns_lock;
  225.  
  226. // fonctions unix/winsock
  227. int hts_read(htsblk* r,char* buff,int size);
  228. //int HTS_TOTAL_RECV_CHECK(int var);
  229. LLint check_downloadable_bytes(int rate);
  230.  
  231. #ifndef HTTRACK_DEFLIB
  232. HTSEXT_API int hts_init(void);
  233. HTSEXT_API int hts_uninit(void);
  234. HTSEXT_API int hts_uninit_module(void);
  235. HTSEXT_API int hts_resetvar(void);  /* dummy */
  236. HTSEXT_API void hts_debug(int level);
  237. HTSEXT_API httrackp* hts_create_opt(void);
  238. HTSEXT_API void hts_free_opt(httrackp *opt);
  239. HTSEXT_API void set_wrappers(httrackp *opt);    /* LEGACY */
  240. HTSEXT_API int plug_wrapper(httrackp *opt, const char *moduleName, const char* argv);
  241.  
  242. HTSEXT_API char* hts_strdup(const char* string);
  243. HTSEXT_API void* hts_malloc(size_t size);
  244. HTSEXT_API void* hts_realloc(void* data, size_t size);
  245. HTSEXT_API void hts_free(void* data);
  246. #endif
  247. extern int hts_dgb_init;
  248. extern FILE* hts_dgb_(void);
  249. #undef _
  250. #define _ ,
  251. #define HTS_DBG(FMT) do {     \
  252.   if (hts_dgb_init > 0) {     \
  253.     FILE *fp = hts_dgb_();    \
  254.     fprintf(fp, FMT);         \
  255.     fprintf(fp, "\n");        \
  256.     fflush(fp);               \
  257.   }                           \
  258. } while(0)
  259.  
  260. // fonctions principales
  261. int http_fopen(httrackp *opt,char* adr,char* fil,htsblk* retour);
  262. int http_xfopen(httrackp *opt,int mode,int treat,int waitconnect,char* xsend,char* adr,char* fil,htsblk* retour);
  263. int http_sendhead(httrackp *opt,t_cookie* cookie,int mode,char* xsend,char* adr,char* fil,char* referer_adr,char* referer_fil,htsblk* retour);
  264. htsblk httpget(httrackp *opt,char* url);
  265. //int newhttp(char* iadr,char* err=NULL);
  266. int newhttp(httrackp *opt,const char* iadr,htsblk* retour,int port,int waitconnect);
  267. HTS_INLINE void deletehttp(htsblk* r);
  268. HTS_INLINE int  deleteaddr(htsblk* r);
  269. HTS_INLINE void deletesoc(T_SOC soc);
  270. HTS_INLINE void deletesoc_r(htsblk* r);
  271. htsblk http_location(httrackp *opt,char* adr,char* fil,char* loc);
  272. htsblk http_test(httrackp *opt,char* adr,char* fil,char* loc);
  273. int check_readinput(htsblk* r);
  274. int check_readinput_t(T_SOC soc, int timeout);
  275. void http_fread(T_SOC soc,htsblk* retour);
  276. LLint http_fread1(htsblk* r);
  277. void treathead(t_cookie* cookie,char* adr,char* fil,htsblk* retour,char* rcvd);
  278. void treatfirstline(htsblk* retour,char* rcvd);
  279. #ifndef HTTRACK_DEFLIB
  280. HTSEXT_API void infostatuscode(char* msg,int statuscode);
  281. #endif
  282.  
  283. // sous-fonctions
  284. htsblk xhttpget(httrackp *opt,char* adr,char* fil);
  285. htsblk http_gethead(httrackp *opt,char* adr,char* fil);
  286. LLint http_xfread1(htsblk* r,int bufl);
  287. HTS_INLINE t_hostent* hts_gethostbyname(httrackp *opt,const char* iadr, void* v_buffer);
  288. #ifndef HTTRACK_DEFLIB
  289. HTSEXT_API t_hostent* vxgethostbyname(char* hostname, void* v_buffer);
  290. #endif
  291. t_hostent* _hts_ghbn(t_dnscache* cache,const char* iadr,t_hostent* retour);
  292. int ftp_available(void);
  293. #if HTS_DNSCACHE
  294. void hts_cache_free(t_dnscache* cache);
  295. int hts_dnstest(httrackp *opt, const char* _iadr);
  296. t_dnscache* _hts_cache(httrackp *opt);
  297. #endif
  298.  
  299. // outils divers
  300. HTS_INLINE TStamp time_local(void);
  301. #ifndef HTTRACK_DEFLIB
  302. HTSEXT_API HTS_INLINE TStamp mtime_local(void);
  303. #endif
  304. void sec2str(char *s,TStamp t);
  305. #ifndef HTTRACK_DEFLIB
  306. HTSEXT_API void qsec2str(char *st,TStamp t);
  307. #endif
  308. void time_gmt_rfc822(char* s);
  309. void time_local_rfc822(char* s);
  310. struct tm* convert_time_rfc822(struct tm* buffer, const char* s);
  311. int set_filetime(const char* file,struct tm* tm_time);
  312. int set_filetime_rfc822(const char* file,const char* date);
  313. int get_filetime_rfc822(const char* file,char* date);
  314. HTS_INLINE void time_rfc822(char* s,struct tm * A);
  315. HTS_INLINE void time_rfc822_local(char* s,struct tm * A);
  316. #ifndef HTTRACK_DEFLIB
  317. HTSEXT_API char* int2char(strc_int2bytes2* strc, int n);
  318. HTSEXT_API char* int2bytes(strc_int2bytes2* strc, LLint n);
  319. HTSEXT_API char* int2bytessec(strc_int2bytes2* strc, long int n);
  320. HTSEXT_API char** int2bytes2(strc_int2bytes2* strc, LLint n);
  321. #endif
  322. HTS_INLINE int sendc(htsblk* r, const char* s);
  323. int finput(int fd,char* s,int max);
  324. int binput(char* buff,char* s,int max);
  325. int linput(FILE* fp,char* s,int max);
  326. int linputsoc(T_SOC soc, char* s, int max);
  327. int linputsoc_t(T_SOC soc, char* s, int max, int timeout);
  328. int linput_trim(FILE* fp,char* s,int max);
  329. int linput_cpp(FILE* fp,char* s,int max);
  330. void rawlinput(FILE* fp,char* s,int max);
  331. char* strstrcase(char *s,char *o);
  332. int ident_url_absolute(const char* url,char* adr,char* fil);
  333. void fil_simplifie(char* f);
  334. int is_unicode_utf8(unsigned char* buffer, unsigned int size);
  335. void map_characters(unsigned char* buffer, unsigned int size, unsigned int* map);
  336. int ishtml(httrackp *opt,const char* urlfil);
  337. int ishtml_ext(const char* a);
  338. int ishttperror(int err);
  339. void guess_httptype(httrackp *opt,char *s,const char *fil);
  340. #ifndef HTTRACK_DEFLIB
  341. HTSEXT_API void get_httptype(httrackp *opt,char *s,const char *fil,int flag);
  342. #endif
  343. int get_userhttptype(httrackp *opt,char *s,const char *fil);
  344. void give_mimext(char *s,const char *st);
  345. #ifndef HTTRACK_DEFLIB
  346. HTSEXT_API int is_knowntype(httrackp *opt,const char *fil);
  347. HTSEXT_API int is_userknowntype(httrackp *opt,const char *fil);
  348. HTSEXT_API int is_dyntype(const char *fil);
  349. HTSEXT_API char* get_ext(char *catbuff, const char *fil);
  350. #endif
  351. int may_unknown(httrackp *opt,const char* st);
  352. #ifndef HTTRACK_DEFLIB
  353. HTSEXT_API char* jump_identification(const char*);
  354. HTSEXT_API char* jump_normalized(const char*);
  355. HTSEXT_API char* jump_toport(const char*);
  356. HTSEXT_API char* fil_normalized(const char* source, char* dest);
  357. HTSEXT_API char* adr_normalized(const char* source, char* dest);
  358. #endif
  359. char* strrchr_limit(const char* s, char c, const char* limit);
  360. char* strstr_limit(const char* s, const char* sub, const char* limit);
  361. HTS_INLINE char* jump_protocol(const char* source);
  362. void code64(unsigned char* a,int size_a,unsigned char* b,int crlf);
  363. #ifndef HTTRACK_DEFLIB
  364. HTSEXT_API void unescape_amp(char* s);
  365. HTSEXT_API void escape_spc_url(char* s);
  366. HTSEXT_API void escape_in_url(char* s);
  367. HTSEXT_API void escape_uri(char* s);
  368. HTSEXT_API void escape_uri_utf(char* s);
  369. HTSEXT_API void escape_check_url(char* s);
  370. HTSEXT_API char* escape_check_url_addr(char *catbuff, const char* s);
  371. HTSEXT_API void x_escape_http(char* s,int mode);
  372. HTSEXT_API void x_escape_html(char* s);
  373. HTSEXT_API void escape_remove_control(char* s);
  374. HTSEXT_API void escape_for_html_print(char* s, char* d);
  375. HTSEXT_API void escape_for_html_print_full(char* s, char* d);
  376. #endif
  377. #ifndef HTTRACK_DEFLIB
  378. HTSEXT_API char* unescape_http(char *catbuff, const char* s);
  379. HTSEXT_API char* unescape_http_unharm(char *catbuff, const char* s, int no_high);
  380. HTSEXT_API char* antislash_unescaped(char *catbuff, const char* s);
  381. HTSEXT_API char* concat(char *catbuff,const char* a,const char* b);
  382. HTSEXT_API char* fconcat(char *catbuff, const char* a, const char* b);
  383. HTSEXT_API char* fconv(char *catbuff, const char* a);
  384. #endif
  385. #define copychar(catbuff,a) concat(catbuff,(a),NULL)
  386. char* fslash(char *catbuff, const char* a);
  387. #ifndef HTTRACK_DEFLIB
  388. HTSEXT_API int hts_log(httrackp *opt, const char* prefix, const char *msg);
  389. #endif
  390.  
  391. char* convtolower(char *catbuff, const char* a);
  392. void hts_lowcase(char* s);
  393. void hts_replace(char *s,char from,char to);
  394. int multipleStringMatch(const char *s, const char *match);
  395.  
  396.  
  397. void fprintfio(FILE* fp,char* buff,char* prefix);
  398.  
  399. #ifdef _WIN32
  400. #else
  401. int sig_ignore_flag( int setflag );     // flag ignore
  402. #endif
  403.  
  404. void cut_path(char* fullpath,char* path,char* pname);
  405. int fexist(const char* s);
  406. /*LLint fsize(const char* s);    */
  407. off_t fpsize(FILE* fp);
  408. off_t fsize(const char* s);    
  409. /* root dir */
  410. #ifndef HTTRACK_DEFLIB
  411. HTSEXT_API char* hts_rootdir(char* file);
  412. #endif
  413.  
  414. // Threads
  415. typedef void* ( *beginthread_type )( void * );
  416. /*unsigned long _beginthread( beginthread_type start_address, unsigned stack_size, void *arglist );*/
  417.  
  418. /* variables globales */
  419. extern HTSEXT_API hts_stat_struct HTS_STAT;
  420. extern int _DEBUG_HEAD;
  421. extern FILE* ioinfo;
  422.  
  423. /* constantes */
  424. extern const char* hts_mime_keep[];
  425. extern const char* hts_mime[][2];
  426. extern const char* hts_main_mime[];
  427. extern const char* hts_detect[];
  428. extern const char* hts_detectbeg[];
  429. extern const char* hts_nodetect[];
  430. extern const char* hts_detectURL[];
  431. extern const char* hts_detectandleave[];
  432. extern const char* hts_detect_js[];
  433.  
  434. // htsmodule.c definitions
  435. extern void* openFunctionLib(const char* file_);
  436. extern void* getFunctionPtr(void* handle, const char* fncname);
  437. extern void  closeFunctionLib(void* handle);
  438.  
  439. extern void clearCallbacks(htscallbacks* chain);
  440. extern size_t hts_get_callback_offs(const char *name);
  441. int hts_set_callback(t_hts_htmlcheck_callbacks *callbacks, const char *name, void *function);
  442. void *hts_get_callback(t_hts_htmlcheck_callbacks *callbacks, const char *name);
  443.  
  444. #define CBSTRUCT(OPT) ((t_hts_htmlcheck_callbacks*) ((OPT)->callbacks_fun))
  445. #define GET_USERCALLBACK(OPT, NAME) ( CBSTRUCT(OPT)-> NAME .fun )
  446. #define GET_USERARG(OPT, NAME) ( CBSTRUCT(OPT)-> NAME .carg )
  447. #define GET_USERDEF(OPT, NAME) ( \
  448.   (CBSTRUCT(OPT) != NULL && CBSTRUCT(OPT)-> NAME .fun != NULL) \
  449.     ? ( GET_USERARG(OPT, NAME) ) \
  450.     : ( default_callbacks. NAME .carg ) \
  451. )
  452. #define GET_CALLBACK(OPT, NAME) ( \
  453.   (CBSTRUCT(OPT) != NULL && CBSTRUCT(OPT)-> NAME .fun != NULL) \
  454.     ? ( GET_USERCALLBACK(OPT, NAME ) ) \
  455.   : ( default_callbacks. NAME .fun ) \
  456. )
  457.  
  458. /* Predefined macros */
  459. #define RUN_CALLBACK_NOARG(OPT, NAME) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME))
  460. #define RUN_CALLBACK0(OPT, NAME) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT)
  461. #define RUN_CALLBACK1(OPT, NAME, ARG1) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1)
  462. #define RUN_CALLBACK2(OPT, NAME, ARG1, ARG2) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2)
  463. #define RUN_CALLBACK3(OPT, NAME, ARG1, ARG2, ARG3) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3)
  464. #define RUN_CALLBACK4(OPT, NAME, ARG1, ARG2, ARG3, ARG4) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3, ARG4)
  465. #define RUN_CALLBACK5(OPT, NAME, ARG1, ARG2, ARG3, ARG4, ARG5) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3, ARG4, ARG5)
  466. #define RUN_CALLBACK6(OPT, NAME, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6)
  467. #define RUN_CALLBACK7(OPT, NAME, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7)
  468. #define RUN_CALLBACK8(OPT, NAME, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8)
  469.  
  470. /*
  471. #define GET_CALLBACK(OPT, NAME, ARG) ( \
  472.   ( \
  473.     ( ARG ) = GET_USERDEF(OPT, NAME), \
  474.     ( \
  475.          (CBSTRUCT(OPT) != NULL && CBSTRUCT(OPT)-> NAME .fun != NULL) \
  476.         ? ( GET_USERCALLBACK(OPT, NAME ) ) \
  477.       : ( default_callbacks. NAME .fun ) \
  478.     ) \
  479.   ) \
  480. )
  481. */
  482.  
  483. #endif    // internals
  484.  
  485. #undef PATH_SEPARATOR
  486. #ifdef _WIN32
  487. #define PATH_SEPARATOR '\\'
  488. #else
  489. #define PATH_SEPARATOR '/'
  490. #endif
  491.  
  492. /* Spaces: CR,LF,TAB,FF */
  493. #define  is_space(c)      ( ((c)==' ') || ((c)=='\"') || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)==11) || ((c)=='\'') )
  494. #define  is_realspace(c)  ( ((c)==' ')                || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)==11)                )
  495. #define  is_taborspace(c) ( ((c)==' ')                                          || ((c)==9)                             )
  496. #define  is_quote(c)      (               ((c)=='\"')                                                    || ((c)=='\'') )
  497. #define  is_retorsep(c)   (                              ((c)==10) || ((c)==13) || ((c)==9)                                          )
  498. //HTS_INLINE int is_space(char);
  499. //HTS_INLINE int is_realspace(char);
  500.  
  501. #define HTTP_IS_REDIRECT(code) ( \
  502.      (code) == 301               \
  503.   || (code) == 302               \
  504.   || (code) == 303               \
  505.   || (code) == 307               \
  506.   )
  507. #define HTTP_IS_NOTMODIFIED(code) ( \
  508.      (code) == 304               \
  509.   )
  510. #define HTTP_IS_OK(code) ( ( (code) / 100 ) == 2 )
  511. #define HTTP_IS_ERROR(code) ( !HTTP_IS_OK(code) && !HTTP_IS_REDIRECT(code) && !HTTP_IS_NOTMODIFIED(code) )
  512.  
  513. // compare le dΘbut de f avec s et retourne la position de la fin
  514. // 'A=a' (case insensitive)
  515. HTS_STATIC int strfield(const char* f,const char* s) {
  516.   int r=0;
  517.   while (streql(*f,*s) && ((*f)!=0) && ((*s)!=0)) { f++; s++; r++; }
  518.   if (*s==0)
  519.     return r;
  520.   else
  521.     return 0;
  522. }
  523. HTS_STATIC int strcmpnocase(char* a,char* b) {
  524.   while(*a) {
  525.     int cmp = hichar(*a) - hichar(*b);
  526.     if (cmp != 0)
  527.       return cmp;
  528.     a++;
  529.     b++;
  530.   }
  531.   return 0;
  532. }
  533.  
  534. #ifdef _WIN32
  535. #define strcasecmp(a,b) stricmp(a,b)
  536. #define strncasecmp(a,b,n) strnicmp(a,b,n)
  537. #endif
  538.  
  539. #define strfield2(f,s) ( (strlen(f)!=strlen(s)) ? 0 : (strfield(f,s)) )
  540.  
  541. // is this MIME an hypertext MIME (text/html), html/js-style or other script/text type?
  542. #define HTS_HYPERTEXT_DEFAULT_MIME "text/html"
  543.  
  544. #if HTS_USEMMS
  545. #define OPT_MMS(a) (strfield2((a), "video/x-ms-asf") != 0)
  546. #else
  547. #define OPT_MMS(a) (0)
  548. #endif
  549. #define is_hypertext_mime__(a) \
  550.   ( (strfield2((a),"text/html")!=0)\
  551.   || (strfield2((a),"application/x-javascript")!=0) \
  552.   || (strfield2((a),"text/css")!=0) \
  553.   /*|| (strfield2((a),"text/vnd.wap.wml")!=0)*/ \
  554.   || (strfield2((a),"image/svg+xml")!=0) \
  555.   || (strfield2((a),"image/svg-xml")!=0) \
  556.   /*|| (strfield2((a),"audio/x-pn-realaudio")!=0) */\
  557.   || (strfield2((a),"application/x-authorware-map")!=0) \
  558.   )
  559. #define may_be_hypertext_mime__(a) \
  560.    (\
  561.      (strfield2((a),"audio/x-pn-realaudio")!=0) \
  562.      || (strfield2((a),"audio/x-mpegurl")!=0) \
  563.      /*|| (strfield2((a),"text/xml")!=0) || (strfield2((a),"application/xml")!=0) : TODO: content check */ \
  564.      || OPT_MMS(a) \
  565.   )
  566.  
  567.  
  568. /* Library internal definictions */
  569. #ifdef HTS_INTERNAL_BYTECODE
  570.  
  571. // check if (mime, file) is hypertext
  572. HTS_STATIC int is_hypertext_mime(httrackp *opt,const char* mime, const char* file) {
  573.   if (is_hypertext_mime__(mime))
  574.     return 1;
  575.   if (may_unknown(opt,mime)) {
  576.     char guessed[256];
  577.     guessed[0] = '\0';
  578.     guess_httptype(opt,guessed, file);
  579.     return is_hypertext_mime__(guessed);
  580.   }
  581.   return 0;
  582. }
  583.  
  584. // check if (mime, file) might be "false" hypertext
  585. HTS_STATIC int may_be_hypertext_mime(httrackp *opt,const char* mime, const char* file) {
  586.   if (may_be_hypertext_mime__(mime))
  587.     return 1;
  588.   if (file != NULL && file[0] != '\0' && may_unknown(opt,mime)) {
  589.     char guessed[256];
  590.     guessed[0] = '\0';
  591.     guess_httptype(opt,guessed, file);
  592.     return may_be_hypertext_mime__(guessed);
  593.   }
  594.   return 0;
  595. }
  596.  
  597. // compare (mime, file) with reference
  598. HTS_STATIC int compare_mime(httrackp *opt,const char* mime, const char* file, const char* reference) {
  599.   if (is_hypertext_mime__(mime) || may_be_hypertext_mime__(mime))
  600.     return strfield2(mime, reference);
  601.   if (file != NULL && file[0] != '\0' && may_unknown(opt,mime)) {
  602.     char guessed[256];
  603.     guessed[0] = '\0';
  604.     guess_httptype(opt,guessed, file);
  605.     return strfield2(guessed, reference);
  606.   }
  607.   return 0;
  608. }
  609.  
  610. #endif
  611.  
  612. #ifdef _WIN32_WCE_XXC
  613. extern char cwd[MAX_PATH+1];
  614. HTS_STATIC char *getcwd_ce(char *buffer, int maxlen) {
  615.     TCHAR fileUnc[MAX_PATH+1];
  616.     char* plast;
  617.     
  618.     if(cwd[0] == 0)
  619.     {
  620.         GetModuleFileName(NULL, fileUnc, MAX_PATH);
  621.         WideCharToMultiByte(CP_ACP, 0, fileUnc, -1, cwd, MAX_PATH, NULL, NULL);
  622.         plast = strrchr(cwd, '\\');
  623.         if(plast)
  624.             *plast = 0;
  625.         /* Special trick to keep start menu clean... */
  626.         if(_stricmp(cwd, "\\windows\\start menu") == 0)
  627.             strcpy(cwd, "\\Apps");
  628.     }
  629.     if(buffer)
  630.         strncpy(buffer, cwd, maxlen);
  631.     return cwd;
  632. }
  633. #undef getcwd
  634. #define getcwd getcwd_ce
  635. #endif
  636.  
  637. #endif
  638.  
  639.  
  640.